<?php


namespace App\Controller;


use App\Request\QuestionSaveRequest;
use App\Service\Instance\JwtInstance;
use App\Service\QuestionService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Elasticsearch\ClientBuilderFactory;

class QuestionController extends AbstractController
{
    #[Inject]
    protected QuestionService $questionService;

    /**
     * 插入数据
     * @param QuestionSaveRequest $questionSaveRequest
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function save(QuestionSaveRequest $questionSaveRequest)
    {
        $uid = JwtInstance::instance()->getId();
        $validated = $questionSaveRequest->validated();
        return $this->response->success($this->questionService->save($uid, $validated['title'], $validated['content']));
    }

    /**
     * es搜索
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function search(){
        $keyword = $this->request->input('keyword', '');
        $pageSize = (int) $this->request->input('pageSize', 10);
        return $this->response->success($this->questionService->search($keyword, $pageSize));
    }

    /**
     * 问题详情
     * @param int $id
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function info(int $id)
    {
        return $this->response->success($this->questionService->info($id));
    }

    /**
     * 问题列表
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function list()
    {
        $pageSize = (int) $this->request->input('pageSize', 10);
        return $this->response->success($this->questionService->list($pageSize));
    }

    /**
     * 热门榜单
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function hotRank()
    {
        return $this->response->success($this->questionService->hotRank());
    }

}